var Pool = require('../Tools/TestCenterPool').pool;
var _ = require('lodash');
var validator = require('validator');
var globalResponseCode = require('../base/ResponseCode');
var moment = require('moment');

class VersionModel {
  async getVersionByProjectId(condition) {
    try {
      let sql = `SELECT id, 
      self_test_case_version, self_test_case_url, name, expect_test_time, 
      expect_version_release_time FROM test_version
        WHERE project_id=${condition.projectId}`;
      // limit ${(condition.page - 1) * condition.size}, ${condition.size}`;

      // let count_sql = `SELECT count(*) FROM test_version where project_id=${condition.projectId}`;
      let versions = await Pool.queryAsync(sql);
      // let count = await Pool.queryAsync(count_sql);


      versions = versions.map((version) => {
        let expectTestTime = moment(version.expect_test_time).format('YYYY-MM-DD');
        let expectVersionReleaseTime = moment(version.expect_version_release_time).format('YYYY-MM-DD');
        return {
          id: version.id,
          name: version.name,
          expectTestTime: expectTestTime,
          expectVersionReleaseTime: expectVersionReleaseTime,
          selfTestCaseVersion: version.self_test_case_version,
          selfTestCaseUrl: version.self_test_case_url
        }
      });
      // count = count.map((c) => {
      //   return c['count(*)'];
      // })[0];
      return versions;
    } catch (error) {
      throw error;
    }
  }

  async insert(condition) {
    try {
      let sql = `INSERT INTO test_version (name, project_id, expect_test_time, 
      expect_version_release_time, self_test_case_version, 
      self_test_case_url, provider_user_id,real_name) SELECT ?,?,?,?,?,?,?,? FROM DUAL WHERE EXISTS(SELECT 1 FROM test_project WHERE id=${condition.projectId})`;

      let sql_params = new Array(8);
      sql_params[0] = condition.name;
      sql_params[1] = condition.projectId;
      sql_params[2] = condition.expectTestTime;
      sql_params[3] = condition.expectVersionReleaseTime;
      sql_params[4] = condition.name;
      sql_params[5] = condition.selfTestCaseUrl;
      sql_params[6] = condition.providerUserId;
      sql_params[7] = condition.createrealname
     // console.log(sql);
      let result = await Pool.queryAsync(sql, sql_params);
      let versionId = result.insertId;
      return versionId;
    } catch (error) {
      throw error;
    }
  }

  async update(version) {
    try {
      let sql = `UPDATE test_version  SET `;
      if (version.name) {
        sql += `name='${version.name}', self_test_case_version='${version.name}',`;
      }
      if (version.expectVersionReleaseTime) {
        sql += `expect_version_release_time='${version.expectVersionReleaseTime}',`;
      }
      if(version.expectTestTime){
        sql += `expect_test_time='${version.expectTestTime}',`;
      }
      if (version.selfTestCaseUrl) {
        sql += `self_test_case_url='${version.selfTestCaseUrl}',`;
      }
      sql = sql.substring(0, sql.lastIndexOf(','));
      sql += ` WHERE id=${version.id} `;
      let result = await Pool.queryAsync(sql);
      return result.affectedRows ? true : false;
    } catch (error) {
      throw error;
    }
  }

  async deleteById(id) {
    try {
      let delete_version_sql = `DELETE FROM test_version WHERE id=${id}`;
      let result = await Pool.queryAsync(delete_version_sql);
      return result.affectedRows;
    } catch (error) {
      return globalResponseCode.DB_ERROR;
    }
  }

  async queryVersionById(id) {
    try {
      let sql = `SELECT v.*,u.user_name,u.real_name,u.role_id,u.role_name 
      FROM test_version v,sys_user u WHERE v.id=${id} AND v.provider_user_id=u.id`;
      let rows = await Pool.queryAsync(sql);
      let version = rows.map((r) => {
        let expectTestTime = moment(r.expect_test_time).format('YYYY-MM-DD');
        let expectVersionReleaseTime = moment(r.expect_version_release_time).format('YYYY-MM-DD');
        return {
          id: r.id,
          projectId: r.project_id,
          name: r.name,
          expectTestTime: expectTestTime,
          expectVersionReleaseTime: expectVersionReleaseTime,
          selfTestCaseVersion: r.self_test_case_version,
          selfTestCaseUrl: r.self_test_case_url,
          providerUserId: r.provider_user_id,
          providerUserName: r.user_name,
          providerRealname: r.real_name,
          recordCount: r.record_count,
          backRecordCount: r.back_record_count
        }
      })[0];
      return version;
    } catch (error) {
      throw error;
    }
  }

  async queryRecordCountByVerisonId(versionId) {
    try {
      let sql = `SELECT record_count FROM test_version WHERE id=${versionId}`;
      let recordCount = await Pool.queryAsync(sql);
      recordCount = recordCount.map((c) => {
        return c['record_count'];
      })[0];
      return recordCount;
    } catch (error) {
      throw error;
    }
  }
}

module.exports = new VersionModel();
let vm = new VersionModel();
vm.queryVersionById(6).then((data) => {
  console.log(data);
})